using System;
using System.Collections;
using System.Collections.Generic;

namespace Genko.Model.Collections
{
	internal class LinkedMemberList : MemberList
	{
		private LinkedList<Member> linkedList;
	
		protected internal LinkedMemberList()
		{
			linkedList = new LinkedList<Member>();
		}
		
		protected LinkedList<Member> LinkedList
		{
			get { return linkedList; }
		}

		public override bool IsReadOnly
		{
			get { return false; }
		}

		public override Member FirstMember
		{
			get
			{
				return LinkedList.First.Value;
			}
		}

		public override Member LastMember
		{
			get
			{
				return LinkedList.Last.Value;
			}
		}

		public override Member this[int index]
		{
			get
			{
				LinkedListNode<Member> node = LinkedList.First;
				int i = 0;
				while (node != null) {
					if (index == i) {
						return node.Value;
					}
					node = node.Next;
					i++;
				}
				throw new IndexOutOfRangeException("Invalid index: " + index);
			}
			set
			{
				LinkedListNode<Member> node = LinkedList.First;
				int i = 0;
				while (node != null) {
					if (index == i) {
						LinkedList.AddAfter(node, value);
						LinkedList.Remove(node);
					}
					node = node.Next;
					i++;
				}
				throw new IndexOutOfRangeException("Invalid index: " + index);
			}
		}

		public override Member this[string memberId]
		{
			get {
				LinkedListNode<Member> node = LinkedList.First;
				while (node != null) {
					if (string.Equals(node.Value.MemberID, memberId)) {
						return node.Value;
					}
					node = node.Next;
				}
				return null;
			}
		}

		public override int IndexOf(Member item)
		{
			throw new NotImplementedException();
		}

		public override void Insert(int index, Member item)
		{
			throw new NotImplementedException();
		}

		public override void RemoveAt(int index)
		{
			throw new NotImplementedException();
		}

		public override void Add(Member item)
		{
			LinkedList.AddLast(item);
		}

		public override void Clear()
		{
			LinkedList.Clear();
		}

		public override bool Contains(Member item)
		{
			return LinkedList.Contains(item);
		}

		public override void CopyTo(Member[] array, int arrayIndex)
		{
			LinkedList.CopyTo(array, arrayIndex);
		}

		public override bool Remove(Member item)
		{
			return LinkedList.Remove(item);
		}

		public override int Count
		{
			get { return LinkedList.Count; }
		}

		public override IEnumerator<Member> GetEnumerator()
		{
			return LinkedList.GetEnumerator();
		}
	}
}
